package com.oocl.euc.core.attachment.repository;

import com.oocl.euc.core.common.ITTestBase;
import com.oocl.euc.core.maintenance.model.Category;
import com.oocl.euc.core.maintenance.model.SubCategory;
import com.oocl.euc.core.maintenance.repository.CategoryRepository;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class CategoryRepositoryIT extends ITTestBase {
    private List<String> categoryIds = new ArrayList<>();
    @Autowired
    CategoryRepository categoryRepository;

    @Before
    public void setUp() throws Exception {
        initDataForTest();
    }

    @After
    public void tearDown() throws Exception {
        removeDataFromDB();
    }

    @Test
    public void should_return_category_with_given_valid_category_id() throws Exception {
        Category category = categoryRepository.findById("categoryId1").get();
        Assert.assertTrue(category != null);
    }


    private void removeDataFromDB() {
        categoryIds.forEach(categoryId -> {
            categoryRepository.deleteById(categoryId);
        });
    }

    private void initDataForTest() {
        createCategory("categoryId1");
    }

    private void createCategory(String categoryId) {
        Category category = getCategory(categoryId);
        List<SubCategory> subCategories = new ArrayList<>();
        subCategories.add(getSubCategory(category, "subCategory1"));
        category.setSubCategory(subCategories);

        Category c = categoryRepository.save(category);
        categoryIds.add(c.getCategoryId());

        Assert.assertTrue(c != null);
    }

    private Category getCategory(String categoryId) {
        Category category = new Category();
        category.setCategoryId(categoryId);
        category.setCategoryName("category");
        category.setSortSeq(1);
        category.setUpdatedAt(new Date());
        return category;
    }

    private SubCategory getSubCategory(Category category, String categoryId) {
        SubCategory subCategory = new SubCategory();
        subCategory.setSubCategoryID(categoryId);
        subCategory.setCategoryID(category.getCategoryId());
        subCategory.setActive(1);
        subCategory.setCategory(category);
        subCategory.setCirculationTo("cirInfo");
        subCategory.setSortSeq("1");
        subCategory.setSubcategoryName("subCategory");
        subCategory.setUpdatedAt(new Date());
        return subCategory;
    }
}